const { ethers } = require("hardhat");

async function main() {
  // 获取部署者的钱包地址
  const [deployer] = await ethers.getSigners();

  // 合约地址，需要替换为实际部署的合约地址
  const contractAddress = "0xFAE0A29160EAd8541A5e02b84ea84201fDA3d160";

  // 创建合约实例
  const MyContract = await ethers.getContractFactory("LKcoin");
  const myContractInstance = MyContract.attach(contractAddress);

  // 调用合约的balanceOf函数并打印结果
  console.log("balanceOf value...");
  const value1 = await myContractInstance.balanceOf(deployer.address);
  console.log("Value1 is:", value1);

  // 手动设置交易的 nonce 值
  const nonce = await deployer.getNonce()
  console.log("Current nonce:", nonce);

  // 转账地址及金额
  recipient = "0xbd29E16d962407c50840d758110e14B1bAcD513f";
  amount = 999988886666;

  // 调用合约的transfer函数并打印结果
  // 方式1
  // result = await myContractInstance.transfer(recipient, amount);
  // console.log("result is:", result);
  
  //方式2
  const data = myContractInstance.interface.encodeFunctionData("transfer",[recipient,amount]);
  const tx = {
    to: contractAddress,
    nonce: nonce,
    data: data,
    gasLimit: 250000, // 根据实际情况调整
    // gasPrice: await deployer.getGasPrice(),
  };
  gasLimit = await deployer.estimateGas(tx);
  console.log("tx: ", tx);
  console.log("Sending transaction...");
  // 使用deployer发送交易
  const txResponse = await deployer.sendTransaction(tx);
  console.log(`Transaction hash: ${txResponse.hash}`);
  // 等待交易被挖矿确认（可选）
  const receipt = await txResponse.wait();
  console.log(`Transaction mined in block ${receipt.blockNumber}`);

  // 调用合约的balanceOf函数并打印结果
  console.log("balanceOf value...");
  const value2 = await myContractInstance.balanceOf(deployer.address);
  console.log("Value2 is:", value2);
}

async function main2() {
  // 获取部署者的钱包地址
  const [deployer] = await ethers.getSigners();

  // 合约地址，需要替换为实际部署的合约地址
  const contractAddress = "0xCbfDa6a539069E5aDE27Da11BA0A0794488c34A8";

  // 创建合约实例
  const MyContract = await ethers.getContractFactory("FflonkVerifier");
  const myContractInstance = MyContract.attach(contractAddress);

  // 调用合约的balanceOf函数并打印结果
  console.log("verifyProof ...");
  const proof = "0x2f5e2d01ecd515c3395a302e0c42d305573bbff81f0886858f27805ca588a4db174aff2fe7c1fb9ac8efca382a1f04bea5ca92f4120e8a158a7bf1e28a637fc516cc06fae73fdb36bc37d910f0c1f1aebdb14d73e412f4c2339528be8d7760a02c07c71d99e64ace957f2d8729573dbd8729ad38bd1a2d286f20269d07a7d8fa2ff676fd55067d230e577a473e4adf219a96abd1c57a953e68993f96691c9d88022bb4f0585443f2c3c92975cb98bc8b7b68cb2e8e9d13a81386aa773e92b46002f1ff548535b917e201a75a0681acf6ab78fbbfa9298d198be4381d227eb0c7028943bd6287c05424d6733cb4082ebe8830c8cc70b58ccc4b0cc326fbfbc4b715e9641d56ab3bdcc48818134bbd48ad7b9b80e4d6fab893114f7dd78a9c99ee1e84136485eb8196c2e6c27618917551a0fa838d69d8af38f9e2e9124c3e4a840ae5362b388e96e6c327238d6164ec994f3c4ab6754aaae4d891ee6dad3b0c64292839389d5ad2968deb880e46049236f190ad2d87717c1ee9d25d7be3a5d7a92d3aab068b30f842b818931a786191d973077727dca5e7034a248500c4f920341d03daf4c4cbe08ba0c68c2c100869e0ae3562e0bfc21d9b72661702d21aa6fe29e54f91c382ee2ce2e09f1ed41f7e4519b2d4e3e2d1b207a4aa6601cfdb21631ca82d47865e49fc445f890384326503c172b73bc013e4f7b0ddfcde1493cb4b1ec1c1d636455eb0f0ba8674d44c70a0ec7c20cf3a08997ab0dbf668128209392bfc39bd0914aab70ccd2b496c950c002030a78992de9347143029e1216a7141300f0755f3378fd5b0b0f4349e05de93177be2802ec3c5f477e83870458bc11e177b6c5624027c8824fcb12805113dfcdef4baceffc750ce26b069727e397e7f2b02d50fbf08a00cedd846bb90107c73bd35eede451c2a2b2f248c9accab850529167e4e36259b2a850c2b385d1f7c44eebe9b40f643ae437011821de066309822aef6a481cddad4cff7dad64cf48d3789510cd820f50ced0bd559cdb1e3cbc9071093e179ed045918115468d6ea4aca061e88a0d5c6b4ddc6d891a00787e60b" 
  // const proofBytes = Buffer.from(proof, 'utf-8');
  // console.log("proofBytes is:", proofBytes);
  const value = await myContractInstance.verifyProof(proof, [2]);
  console.log("Value is:", value);
}

main()
  .then(() => process.exit(0))
  .catch(error => {
    console.error(error);
    process.exit(1);
  });
